package cn.study.threadlocalTest;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 跟大佳一起学
 * 类说明：ThreadLocalMemoryLeak 内存泄露
 */
public class ThreadLocalMemoryLeak {
    private static final int TASK_LOOP_SIZE = 5000;
    /*线程池*/
    final static ThreadPoolExecutor poolExecutor =
            new ThreadPoolExecutor(5, 5, 1,
            TimeUnit.MINUTES,
            new LinkedBlockingQueue<>());
    static class LocalVariable {
        private byte[] a = new byte[1024*1024*50];/*50M大小的数组*/
    }
    ThreadLocal<LocalVariable> threadLocalLV;

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < TASK_LOOP_SIZE; ++i) {
            poolExecutor.execute(new Runnable() {
                public void run() {
                    ThreadLocalMemoryLeak oom = new ThreadLocalMemoryLeak();
                    oom.threadLocalLV = new ThreadLocal<>();
                    oom.threadLocalLV.set(new LocalVariable());
                    System.out.println("执行行数");
//                    oom.threadLocalLV.remove();
                }

            });
        }
    }
}
